/*
Copyright © 2023 ywc 522350630@qq.com
*/
package cmd

import (
	"log"
	"net"

	_ "gitee.com/yangwencheng/vblog/apps"
	"gitee.com/yangwencheng/vblog/config"
	"gitee.com/yangwencheng/vblog/ioc"
	"github.com/spf13/cobra"
	"google.golang.org/grpc"
)

var (
	grpcConfigType string
	grpcConfigPath string
)

// serverCmd represents the server command
var grpcCmd = &cobra.Command{
	Use:   "grpc server",
	Short: "blog grpc",
	Long:  `比较长的一个描述.`,
	Run: func(cmd *cobra.Command, args []string) {

		// 初始化配置文件
		var err error
		switch grpcConfigType {
		case "file":
			_, err = config.GetConfigFromFile(configPath)
		case "env":
			_, err = config.GetConfigFromEnv()
		}
		cobra.CheckErr(err)
		grpcServer := grpc.NewServer()

		// 初始化控制器
		err = ioc.App().InitController()
		cobra.CheckErr(err)
		// 初始化api
		err = ioc.App().InitGrpc(grpcServer)
		cobra.CheckErr(err)

		lis, err := net.Listen("tcp", ":1234")
		if err != nil {
			log.Fatal(err)
		}
		grpcServer.Serve(lis)
	},
}

func init() {
	rootCmd.AddCommand(grpcCmd)
	grpcCmd.Flags().StringVarP(&grpcConfigType, "config-type", "t", "file", "程序加载配置的方式")
	grpcCmd.Flags().StringVarP(&grpcConfigPath, "config-path", "p", "etc", "配置文件的目录")
}
